﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsPresenter.AccountingObjectCategory;
using WindowsFormsModel.BusinessObjects;
using Utils;
using Infragistics.Win.UltraWinTree;
using Infragistics.Win.UltraWinEditors;
using WindowsFormsPresenter.AccountingObject;

namespace WindowsFormApplication.FormCategoryTreeView
{
    public partial class frmVendorList : frmBaseTreeView, IAccountingObjectCategoriesView, IAccountingObjectsView
    {
        #region Presenter

        private AccountingObjectCategoriesPresenter _accountingObjectCategoriesPresenter;
        private AccountingObjectsPresenter _accountingObjectsPresenter;
        private bool isCustomer = false;

        #endregion

        private static int totalPage = 0;
        private static int currentPage = 0;

        #region Field

        public IList<AccountingObjectCategoryModel> AccountingObjectCategories
        {
            set
            {
                DataSet ds = new DataSet();
                ds = value.AsDataSet(TableNameTree);

                ds.Relations.Add(RelationName, ds.Tables[TableNameTree].Columns[TreePrimaryKey], ds.Tables[TableNameTree].Columns[TreeParentKey], false);
                treeMain.DataSource = ds.Tables[TableNameTree];

                UltraTreeNode node = new UltraTreeNode();
                node = treeMain.TopNode;

                if (treeMain.Nodes.Count > 0)
                {
                    while (node != null)
                    {
                        node.DataKey = node.Cells[TreePrimaryKey].Value;
                        node.Key = node.DataKey.ToString();
                        node = node.NextVisibleNode;
                    }
                }
                docTree.ControlPanes[0].Text = "Danh sách loại NCC";
            }
        }

        public IList<AccountingObjectModel> AccountingObjects
        {
            set
            {
                var accountingObjects = value;
                grdMain.DataSource = accountingObjects;

                for (int i = 0; i < grdMain.DisplayLayout.Bands[0].Columns.Count; i++) { grdMain.DisplayLayout.Bands[0].Columns[i].Hidden = true; }

                grdMain.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["Address"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["Tel"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["IsActive"].Hidden = false;

                grdMain.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Header.Caption = "Mã nhà cung cấp";
                grdMain.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Header.Caption = "Tên nhà cung cấp";
                grdMain.DisplayLayout.Bands[0].Columns["Address"].Header.Caption = "Địa chỉ";
                grdMain.DisplayLayout.Bands[0].Columns["Tel"].Header.Caption = "Số điện thoại";
                grdMain.DisplayLayout.Bands[0].Columns["IsActive"].Header.Caption = "Được sử dụng";
            }
        }

        #endregion

        protected override void InitData()
        {
            try
            {
                _accountingObjectCategoriesPresenter.DisplayActive(isCustomer);
                totalPage = _accountingObjectsPresenter.GetTotalPage(isCustomer);
                _accountingObjectsPresenter.DisplayPageByBranch(currentPage, isCustomer);
                toolbarsManager.Tools["lblPage"].SharedProps.Caption = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void NextPage()
        {
            if (currentPage < totalPage)
            {
                currentPage = currentPage + 1;
                InitData();
            }
        }

        protected override void PreviousPage()
        {
            if (currentPage > 0)
            {
                currentPage = currentPage - 1;
                InitData();
            }
        }

        protected override void DeleteRow()
        {
            try
            {
                if (grdMain.ActiveRow.Index >= 0)
                {
                    string accountingObjectId = grdMain.ActiveRow.Cells[TablePrimaryKey].Value.ToString();
                    new AccountingObjectPresenter(null).Delete(accountingObjectId);
                    currentPage = 0;
                    ShowMessageBox("Xóa dữ liệu hoàn thành !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowMessageBox("Bạn chưa chọn dòng để xóa !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void FilterObject(string keyValue)
        {
            try
            {
                totalPage = _accountingObjectsPresenter.GetTotalPageByCategory(keyValue);
                _accountingObjectsPresenter.DisplayPageByBranchAndCategory(currentPage, keyValue);
                toolbarsManager.Tools["lblPage"].SharedProps.Caption = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public frmVendorList()
        {
            InitializeComponent();

            _accountingObjectCategoriesPresenter = new AccountingObjectCategoriesPresenter(this);
            _accountingObjectsPresenter = new AccountingObjectsPresenter(this);
        }
    }
}
